package com.example.app

import android.os.Bundle
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.viewinterop.AndroidView
import com.example.app.ui.theme.AppTheme


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 在布局文件中使用compose组件
        setContentView(R.layout.main)
        // 获取composeView
        val composeView = findViewById<ComposeView>(R.id.compose_view)

        // 将内容放入composeView
        composeView.setContent {
            Column {
                Greeting("Android")
                AndroidView(factory = {
                    // 原生安卓组件
                    TextView(it)
                }) {
                    it.apply {
                        text = "ComposeView 下的 textView"
                    }
                }
            }
        }

//        setContent {
//            MessageCard(msg = Message("ldn", "yxy"))
//            AppTheme {
//                // A surface container using the 'background' color from the theme
//                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background) {
//                    Greeting("Android")
//                }
//                Greeting("Android")
//                showAddress("天津市")
//                Text("ldn")
//            }
//        }
    }
}

data class Message(val author: String, val body: String)

@Composable
fun MessageCard(msg: Message) {
    Row {
        Image(painter = painterResource(id = R.drawable.ic_launcher_foreground), contentDescription ="android" )
        Column {
            Text(text = msg.author)
            Text(text = msg.body)
        }
    }
}

@Composable
fun Greeting(name: String) {
    Column {
        Text(text = "Hello $name!")
    }
}

@Composable
fun showAddress(str: String) {
    Box {
        Text(text = "地址是 $str")
    }
}
// 预览
@Preview(showBackground = true, showSystemUi = true)
@Composable
fun DefaultPreview() {
    AppTheme {
        Greeting("Android")
    }
}